<?php
/**
 * @since v1.0
 * @modify 2015.6.21
 * @copyright 深圳亿游
 */
require_once(LIB_PATH.'controller/root.con.php');

class studentCon extends rootCon
{
    public function infoAction(){
        $uid = mylogin::getUid(true);
        $obj = new Student();
        $obj->load();
        $sql = "SELECT * FROM ".$obj->table()." WHERE ".$obj->key()."=".$obj->studentId." AND uid=$uid";
        $rect = $obj->one($sql);

        if(!$rect) $this->j(-1,'没有找到相关信息',[$obj->key()=>$obj->studentId]);

        $this->j(0,'',$obj->hide($rect));
    }
	
	/**
	 * 学生年级
	 */
    public function typeAction() {
        //$uid = mylogin::getUid(true);

        $obj = new Grade();
        $result = $obj->all(['status',STATUS_PUBLISH],'`sort` ASC');

        $rect['grade'] = $result;

        $this->j(0, "type",$rect);
    }

    public function saveAction(){
        $uid = mylogin::getUid(true);
        $obj = new Student();
        $obj->load();

        if($obj->studentId){//检查修改权限
            $find = $obj->find([$obj->key(),$obj->studentId]);
            if(!$find) $this->j(-3,'error key',$obj->key());
            if($obj->uid != $uid) $obj->j(-4,'error uid');
            $obj->load();
        }

        if(is_numeric($obj->studentImg)){
            $img = new ImageFile();
            $img->find($obj->studentImg);
            $obj->studentImg = $img->uploadImg;
        }

        $obj->uid = $uid;
        $obj->ip = myrequest::getIP();
        $obj->created = date('Y-m-d H:i:s');
        $obj->save();

        $this->j(0,'',$obj->hide($obj->arr));
    }
	
	/**
	 * 我的学生
	 */
    public function listAction(){
        $uid = mylogin::getUid(true);
        $obj = new Student();
        $obj->load();
        $obj->uid = $uid;
        $con = $obj->getConByLoad();
        $all = $obj->all($con);

        //获取评测结果
        $i = 0;
        foreach($all as $item){
            if(!$item['studentImg'])   $all[$i]['studentImg'] = "/img/addstudent.png";

            $all[$i]['question'] = $this->getQeustionResult($item['studentId']);
            $i++;
        }

        $this->j(0,'',$all);
    }

    public function getQeustionResult($studentId){
        $questionStudent = new QuestionStudent();
        $sql = "SELECT * FROM ".$questionStudent->table()." WHERE studentId=$studentId ORDER BY questionTypeId DESC, score ASC LIMIT 3";
        return $questionStudent->query($sql);
    }

}



